package com.softtown.modules.sys.controller;

import com.softtown.common.annotation.LogOperation;
import com.softtown.common.constant.Constant;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.page.PageData;
import com.softtown.common.regularUtils.RegularUtils;
import com.softtown.common.utils.Result;
import com.softtown.modules.sys.dto.CompSysRoleDTO;
import com.softtown.modules.sys.entity.CompSysRoleEntity;
import com.softtown.modules.sys.entity.CompSysRoleUserEntity;
import com.softtown.modules.sys.service.CompSysRoleDataScropeService;
import com.softtown.modules.sys.service.CompSysRoleMenuService;
import com.softtown.modules.sys.service.CompSysRoleService;
import com.softtown.modules.sys.service.CompSysRoleUserService;
import com.softtown.modules.sys.vo.CompSysRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @version v1.0
 * @ProjectName: softtown-aioffice
 * @ClassName: CompSysRoleController
 * @Description: 公司-角色Controller层
 * @Author: fuming
 * @Date: 2019/6/4 14:30
 */
@RestController
@RequestMapping("comp/role")
@Api(tags = "公司角色")
public class CompSysRoleController {

    @Resource
    private CompSysRoleService compSysRoleService;

    @Resource
    private CompSysRoleUserService compSysRoleUserService;

    @Resource
    private CompSysRoleMenuService compSysRoleMenuService;

    @Resource
    private CompSysRoleDataScropeService compSysRoleDataScropeService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "compId", value = "公司id", paramType = "query", dataType = "String")
    })
    public Result<PageData<CompSysRoleDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<CompSysRoleDTO> page = compSysRoleService.page(params);
        return new Result<PageData<CompSysRoleDTO>>().ok(page);
    }

    /**
     * @param @param [companyId]
     * @return @return com.softtown.common.utils.Result<java.util.List<com.softtown.modules.sys.vo.CompSysRoleVO>>
     * @throws
     * @Title: getCompRolesList
     * @Description: 根据所属公司的ID获取该公司下的所有角色
     * @author fuming
     * @date 2019/6/5 10:11
     */
    @PostMapping("getCompRoles")
    @ApiOperation("获取公司下的角色")
    @LogOperation("获取公司下的角色")
    public Result<List<CompSysRoleVO>> getCompRolesList(@RequestBody String companyId) {
        String compId = JSONObject.fromObject(companyId).get("companyId").toString();
        List<CompSysRoleVO> rolesListByCompId = compSysRoleService.getListByCompId(Long.valueOf(compId));
        return new Result<List<CompSysRoleVO>>().ok(rolesListByCompId);
    }

    @PostMapping("activateRole")
    @ApiOperation("角色激活/禁用")
    @LogOperation("角色激活/禁用")
    public Result activateRole(@RequestBody String id) {
        String roleId = JSONObject.fromObject(id).get("id").toString();
        return compSysRoleService.activate(Long.valueOf(roleId));
    }

    @PostMapping("deleteRole")
    @ApiOperation("角色删除")
    @LogOperation("角色删除")
    public Result deleteRole(@RequestBody String id) {
        Result result = new Result<>();
        String roleId = JSONObject.fromObject(id).get("id").toString();
        List<CompSysRoleUserEntity> compSysRoleUserEntity = compSysRoleUserService.getByRoleId(Long.valueOf(roleId));
        if (compSysRoleUserEntity.size() > 0) {
            return result.error(ErrorCode.DATA_EXISTS);
        } else {
            compSysRoleService.deleteRole(Long.valueOf(roleId));
            return result;
        }
    }

    @PostMapping("roleInfo")
    @ApiOperation("角色信息")
    @LogOperation("角色信息")
    public Result roleInfo(@RequestBody String id) {
        String roleId = JSONObject.fromObject(id).get("id").toString();
        CompSysRoleDTO data = compSysRoleService.get(Long.valueOf(roleId));

        //查询角色对应的菜单
        List<Long> menuIdList = compSysRoleMenuService.getMenuIdList(Long.valueOf(roleId));
        data.setMenuIdList(menuIdList);

        //查询角色对应的数据权限
        List<Long> deptIdList = compSysRoleDataScropeService.getDeptIdList(Long.valueOf(roleId));
        data.setDeptIdList(deptIdList);
        return new Result<CompSysRoleDTO>().ok(data);
    }

    @PostMapping("updateRoleInfo")
    @ApiOperation("角色信息修改")
    @LogOperation("角色信息修改")
    public Result updateRoleInfo(@RequestBody CompSysRoleDTO compSysRoleDTO) {
        Result result = new Result<>();
        if (RegularUtils.isCompName(compSysRoleDTO.getName())) {
            return result.error(ErrorCode.ROLE_NAME_TYPE);
        }
        if (!RegularUtils.isNum(String.valueOf(compSysRoleDTO.getSort()))) {
            return result.error(ErrorCode.INCORRECT_SORT_FORMAT);
        }
        CompSysRoleEntity compSysRoleEntityz = compSysRoleService.getByCompIdAndRoleName(compSysRoleDTO.getCompId(), compSysRoleDTO.getName());
        if (compSysRoleEntityz != null) {
            if (compSysRoleEntityz.getId().equals(compSysRoleDTO.getId())) {
                compSysRoleService.updateRoleInfo(compSysRoleDTO);
            } else {
                return result.error(ErrorCode.ROLE_NAME_EXISTS);
            }
        } else {
            compSysRoleService.updateRoleInfo(compSysRoleDTO);
        }
        return result;
    }

    @PostMapping("addRoleInfo")
    @ApiOperation("角色增加")
    @LogOperation("角色增加")
    public Result addRoleInfo(@RequestBody CompSysRoleDTO compSysRoleDTO) {
        Result result = new Result<>();
        if (RegularUtils.isCompName(compSysRoleDTO.getName())) {
            return result.error(ErrorCode.ROLE_NAME_TYPE);
        }
        if (!RegularUtils.isNum(String.valueOf(compSysRoleDTO.getSort()))) {
            return result.error(ErrorCode.INCORRECT_SORT_FORMAT);
        }
        CompSysRoleEntity compSysRoleEntityz = compSysRoleService.getByCompIdAndRoleName(compSysRoleDTO.getCompId(), compSysRoleDTO.getName());
        if (compSysRoleEntityz != null) {
            return result.error(ErrorCode.ROLE_NAME_EXISTS);
        } else {
            compSysRoleService.addRoleInfo(compSysRoleDTO);
        }
        return result;
    }

    @PostMapping("selectList")
    @ApiOperation("角色下拉列表")
    @LogOperation("角色下拉列表")
    public Result getRoleSelectList(@RequestBody Map<String, Object> map) {
        String id = JSONObject.fromObject(map).get("companyId").toString();
        List<CompSysRoleVO> list = compSysRoleService.getListByCompId(Long.valueOf(id));
        return new Result<List<CompSysRoleVO>>().ok(list);
    }
}
